package com.mycar2w.cloud.service.serviceimpl;

import com.mycar2w.cloud.entity.TUser;
import com.mycar2w.cloud.service.pojo.TUserInfo;
import com.mycar2w.cloud.service.repository.TDicAddressRepository;
import com.mycar2w.cloud.service.repository.TDicGenderRepository;
import com.mycar2w.cloud.service.repository.TUserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Date;

/**
 * created time 2017/4/2
 *
 * @author ljc
 * @version V1.0
 */
@Service
public class UicService {
    @Autowired
    private TUserRepository tUserRepository;
    @Autowired
    private TDicGenderRepository tDicGenderRepository;
    private TDicAddressRepository tDicAddressRepository;

    /**
     * 注册服务
     * @param tUser
     */
    public void register(TUser tUser){
        tUser.setCreateTime(new Timestamp(System.currentTimeMillis()));
        tUser.setLastModifiedTime(new Timestamp(System.currentTimeMillis()));
        tUser.setRole("1");
        tUser.setIsAble(1);
        tUser.setId(new Date().getTime());
        tUserRepository.save(tUser);
    }

    /**
     * 验证是否该手机号已经被注册
     * @param phone
     * @return
     */
    public boolean isRegister(String phone) {
        if(tUserRepository.findByPhone(phone)!=null){
            return true;
        }
        return false;
    }

    /**
     * 登陆
     * @param phone 电话
     * @param password 密码
     * @return 个人信息 TUser
     */
    public TUser login(String phone, String password) throws Exception{

        TUser tUser=tUserRepository.findByPhoneAndPassword(phone,password);
        return tUser;
    }

    /**
     * 修改密码
     * @param phone 电话
     * @param newpassword 密码
     */
    public void changePassword(String phone,String newpassword){
        TUser tUser=tUserRepository.findByPhone(phone);
        tUser.setPassword(tUser.getPassword());
        tUserRepository.saveAndFlush(tUser);

    }

    /**
     * 获得个人详细信息
     * @param phone
     * @return
     */
    public TUserInfo getDetail(String phone) {
        TUser tUser=tUserRepository.findByPhone(phone);
        TUserInfo tUserInfo=new TUserInfo();
        tUserInfo.setNickname(tUser.getNickname());
        tUserInfo.setGender(tUser.getGenderId());
        tUserInfo.setAddress(tUser.getAddressId());
        tUserInfo.setProtrait(tUser.getProtrait());
        tUserInfo.setEmail(tUser.getEmail());
        tUserInfo.setIdentification(tUser.getIdentification());
        tUserInfo.setOccupation(tUser.getOccupation());
        tUserInfo.setAge(tUser.getAge());
        return tUserInfo;
    }

    /**
     * 修改个人信息
     * @param phone 电话
     * @param tUserInfo 信息主体
     */
    public void detailUpdate(String phone, TUserInfo tUserInfo) {
        TUser tUser=tUserRepository.findByPhone(phone);
        tUser.setNickname(tUserInfo.getNickname());
        tUser.setGenderId(tUserInfo.getGender());
        tUser.setAddressId(tUserInfo.getAddress());
        tUser.setEmail(tUserInfo.getEmail());
        tUser.setIdentification(tUserInfo.getIdentification());
        tUser.setOccupation(tUserInfo.getOccupation());
        tUser.setAge(tUserInfo.getAge());
        tUserRepository.saveAndFlush(tUser);
    }

    public boolean isRegisterEmail(String email) {
        if(tUserRepository.findByEmail(email)!=null&&tUserRepository.findByEmail(email).size()!=0){
            return true;
        }
        return false;
    }

    public boolean findByPhoneAndOldPasswore(String phone, String oldPassword) {
        if(tUserRepository.findByPhoneAndPassword(phone,oldPassword)!=null){
            return true;
        }
        return false;
    }
}
